﻿namespace SEED.SEEDControllers {
    using System;
    using SEEDModels;
    using System.Linq;
    using System.Collections.Generic;

    public class SEEDReader {
        private SEED seed;

        public SEEDReader() { }
        public SEEDReader(string fileName) {
            seed = new SEED(fileName);
        }
        /// <summary>
        /// 获取所有台网名
        /// </summary>
        /// <returns>返回台网名列表</returns>
        public string[] GetNetworkCodes() {
            var list = (from network in seed.Stations
                       orderby network.NetworkCode
                       select network.NetworkCode).Distinct();
            return list.ToArray<string>();
        }
        /// <summary>
        /// 通过台网名称获取该台网的所有台站名
        /// </summary>
        /// <param name="networkCode">台网名</param>
        /// <returns>返回台站名称列表</returns>
        public string[] GetStationNameByNetCode(string networkCode) {
            var list = from station in seed.Stations
                       where station.NetworkCode == networkCode
                       select station.Name;
            return list.Distinct().ToArray<string>();
        }
        /// <summary>
        /// 获取特定台网的台站的所有通道的名称
        /// </summary>
        /// <param name="networkCode">台网名</param>
        /// <param name="stationName">台站名</param>
        /// <returns></returns>
        public string[] GetChannelNameByStationName(string networkCode, string stationName) {
            var station = (from sta in seed.Stations
                           where sta.NetworkCode == networkCode && sta.Name == stationName
                           select sta).SingleOrDefault();
            var list = from channel in station.Channels
                       select channel.Name;
            return list.Distinct().ToArray<string>();
        }

        public int[] GetWaveformData() {
            return seed.Waveform[0].WaveData;
        }
        /// <summary>
        /// 根据台网名获取其所有台站
        /// </summary>
        /// <param name="networkCode">台网名</param>
        /// <returns>台站列表</returns>
        public IEnumerable<Station> GetStationsByNetwork(string networkCode) {
            var station = from sta in seed.Stations
                          where sta.NetworkCode == networkCode
                          select sta;
            return station;
        }
        /// <summary>
        /// 通过台网名和台站名获取其所有的通道
        /// </summary>
        /// <param name="networkCode">台网名</param>
        /// <param name="staName">台站名</param>
        /// <returns>对应的台站列表</returns>
        public IEnumerable<Channel> GetChannelByStaNnetwordCode(string networkCode, string staName) {
            var station = (from sta in seed.Stations
                           where sta.NetworkCode == networkCode && sta.Name == staName
                           select sta).FirstOrDefault();
            return station.Channels;
        }
        /// <summary>
        /// 通过台网名台站名和通道名获取通道实例
        /// </summary>
        /// <param name="networkCode">台网名</param>
        /// <param name="staName">台站名</param>
        /// <param name="chaName">通道名</param>
        /// <returns></returns>
        public Channel GetSingleChannel(string networkCode, string staName, string chaName) {
            var channels = GetChannelByStaNnetwordCode(networkCode, staName);
            var channel = (from cha in channels
                           where cha.Name == chaName
                           select cha).FirstOrDefault();
            return channel;
        }
        /// <summary>
        /// 获取特定台站的所有通道波形数据
        /// </summary>
        /// <param name="netCode">台网名</param>
        /// <param name="staName">台站名</param>
        /// <returns>对应的所有通道波形数据</returns>
        public IEnumerable<Waveform> GetWaveFormByNetCodeNStaName(string netCode, string staName) {
            var list = from wave in seed.Waveform
                       where wave.NetworkCode == netCode && wave.StationName == staName
                       select wave;
            return list;
        }
        /// <summary>
        /// 通过通道名获取其对应的波形数据
        /// </summary>
        /// <param name="netCode">台网名</param>
        /// <param name="staName">台站名</param>
        /// <param name="chaName">通道名</param>
        /// <returns>通道对应的波形数据</returns>
        public Waveform GetWaveFormByChannelName(string netCode, string staName, string chaName) {
            var list = GetWaveFormByNetCodeNStaName(netCode, staName);
            var wave = (from w in list
                       where w.ChannelName == chaName
                       select w).FirstOrDefault();
            return wave;
        }

    }
}
